﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;

using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.ServiceHelper.Excel;

namespace B2CProject
{
    /// <summary>
    /// 上传单据体数据文件
    /// </summary>
    /// <remarks>
    /// 界面说明：
    /// 1. 添加一个文件上传控件，设置文件后缀，必须为*.xls
    /// 2. 添加确定、取消两个按钮
    /// 3. 默认情况下，确定按钮时锁定的，只有上传文件完毕，才解锁
    /// </remarks>
    [Description("上传单据体数据文件")]
   public class ImportFileUpdateEdit : AbstractDynamicFormPlugIn
    {
        /// <summary>
        /// 上传上来的文件名：完整的文件名，包含了物理路径
        /// </summary>
        private string _fullFileName = string.Empty;
        public override void AfterBindData(EventArgs e)
        {
            this.View.GetControl("F_JD_BTNOK").Enabled = false;
        }

                /// <summary>
        /// 文件上传完毕，触发此事件：通过此事件获取上传上来的文件名
        /// </summary>
        /// <param name="e"></param>
        public override void CustomEvents(CustomEventsArgs e)
        {
            if (e.Key.EqualsIgnoreCase("F_PAEZ_FileUpdate"))
            {
                this.View.GetControl("F_PAEZ_FileUpdate").SetCustomPropertyValue("NeedCallback", true);
                this.View.GetControl("F_PAEZ_FileUpdate").SetCustomPropertyValue("IsRequesting", false);

                if (e.EventName.EqualsIgnoreCase("FileChanged"))
                {
                    // 文件上传完毕
                    // 取文件上传参数，文件名
                    JSONObject uploadInfo = KDObjectConverter.DeserializeObject<JSONObject>(e.EventArgs);

                    if (uploadInfo != null)
                    {
                        JSONArray json = new JSONArray(uploadInfo["NewValue"].ToString());

                        if (json.Count > 0)
                        {
                            // 取上传的文件名
                            string fileName = (json[0] as Dictionary<string, object>)["ServerFileName"].ToString();

                            this._fullFileName = this.GetFullFileName(fileName);
                            // 解锁确定按钮
                            this.View.GetControl("F_JD_BTNOK").Enabled = true;
                        }
                        else
                        {
                            // 锁定确定按钮
                            this.View.GetControl("F_JD_BTNOK").Enabled = false;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 按钮点击事件
        /// </summary>
        /// <param name="e"></param>
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            if (e.Key.EqualsIgnoreCase("F_JD_BTNOK"))
            {// 确定
                this.View.ReturnToParentWindow(new FormResult(this._fullFileName));
                this.View.Close();
            }
            else if (e.Key.EqualsIgnoreCase("F_JD_BTNCancel"))
            {// 取消
                this.View.Close();
            }
        }

        /// <summary>
        /// 产生完整的文件名，包含了物理路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private string GetFullFileName(string fileName)
        {
            string dir = "FileUpLoadServices\\UploadFiles";
            return PathUtils.GetPhysicalPath(dir, fileName);
        }


    }
}
